package org.tony.sim.fmt;

public class FmtB extends Fmt32 {
    // B类型的指令IMM实际使用时要在最末尾拼接一个1'b0
    private static final byte[] IMM_BITS = new byte[]{31, 7, 30, 29, 28, 27, 26, 25, 11, 10, 9, 8};

    protected FmtB(Module module, Category category, String name, String description, int inst) {
        super(module, category, name, description, inst);
    }

    public byte getFunct3() {
        return (byte) getBits(super.inst, FUNCT3_BITS);
    }

    public void setFunct3(int funct3) {
        super.inst = setBits(super.inst, funct3, FUNCT3_BITS);
    }

    public byte getRs1() {
        return (byte) getBits(super.inst, RS1_BITS);
    }

    public void setRs1(int rs1) {
        super.inst = setBits(super.inst, rs1, RS1_BITS);
    }

    public byte getRs2() {
        return (byte) getBits(super.inst, RS2_BITS);
    }

    public void setRs2(int rs2) {
        super.inst = setBits(super.inst, rs2, RS2_BITS);
    }

    public short getImm() {
        return (short) getBits(super.inst, IMM_BITS);
    }

    public void setImm(int imm) {
        super.inst = setBits(super.inst, imm, IMM_BITS);
    }

    @Override
    public String toString() {
        String ret = super.toString();
        StringBuilder stringBuilder = new StringBuilder(ret.substring(0, ret.length() - 1));
        stringBuilder.append(", \"opcode\":\"").append(transToBinaryString(getOpcode(), OPCODE_BITS.length))
                .append("\", \"funct3\":\"").append(transToBinaryString(getFunct3(), FUNCT3_BITS.length))
                .append("\", \"rs1\":\"").append(transToBinaryString(getRs1(), RS1_BITS.length))
                .append("\", \"rs2\":\"").append(transToBinaryString(getRs2(), RS2_BITS.length))
                .append("\", \"imm\":\"").append(transToBinaryString(getImm(), IMM_BITS.length))
                .append("\"}");
        return stringBuilder.toString();
    }
}
